package com.it.schoolbookshop_back.mapper;

import com.it.schoolbookshop_back.entities.po.Notification;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 通知数据访问接口
 */
@Mapper
public interface NotificationMapper {
    
    /**
     * 添加通知
     * @param notification 通知对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO notifications(user_id, type, content, related_id, is_read) " +
            "VALUES(#{userId}, #{type}, #{content}, #{relatedId}, #{isRead})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertNotification(Notification notification);
    
    /**
     * 根据用户ID获取所有通知
     * @param userId 用户ID
     * @return 通知列表
     */
    @Select("SELECT * FROM notifications WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<Notification> getNotificationsByUserId(@Param("userId") Integer userId);
    
    /**
     * 根据用户ID和通知类型获取通知
     * @param userId 用户ID
     * @param type 通知类型
     * @return 通知列表
     */
    @Select("SELECT * FROM notifications WHERE user_id = #{userId} AND type = #{type} ORDER BY created_at DESC")
    List<Notification> getNotificationsByUserIdAndType(@Param("userId") Integer userId, @Param("type") String type);
    
    /**
     * 标记通知为已读
     * @param notificationId 通知ID
     * @return 影响的行数
     */
    @Update("UPDATE notifications SET is_read = 1 WHERE id = #{notificationId}")
    int markAsRead(@Param("notificationId") Integer notificationId);
    
    /**
     * 标记用户所有通知为已读
     * @param userId 用户ID
     * @return 影响的行数
     */
    @Update("UPDATE notifications SET is_read = 1 WHERE user_id = #{userId}")
    int markAllAsRead(@Param("userId") Integer userId);
    
    /**
     * 获取用户未读通知数量
     * @param userId 用户ID
     * @return 未读通知数量
     */
    @Select("SELECT COUNT(*) FROM notifications WHERE user_id = #{userId} AND is_read = 0")
    int getUnreadCount(@Param("userId") Integer userId);
    
    /**
     * 根据通知类型获取用户未读通知数量
     * @param userId 用户ID
     * @param type 通知类型
     * @return 未读通知数量
     */
    @Select("SELECT COUNT(*) FROM notifications WHERE user_id = #{userId} AND type = #{type} AND is_read = 0")
    int getUnreadCountByType(@Param("userId") Integer userId, @Param("type") String type);
    
    /**
     * 删除通知
     * @param notificationId 通知ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM notifications WHERE id = #{notificationId}")
    int deleteNotification(@Param("notificationId") Integer notificationId);
} 